package com.fyzk.activity.forum;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.fyzk.activity.app.ActivityTaskManager;
import com.fyzk.activity.user.LoginActivity;
import com.fyzk.data.AppLotteryInfos;
import com.fyzk.data.preference.MySharedPreferences;
import com.fyzk.view.ViewCallBack;
import com.tyjc.lottery.R;
import com.fyzk.utils.Utils;

import http.DataUtil.text.TextUtil;

/**
 * Created by fanxs11 on 16/10/20.
 */

public class HeadView extends LinearLayout {

    private Context context;

    private ForumUserInfoBean info;

    private View view;
    private ImageView headportrait_iv;  // 头像
    private LinearLayout center_ll;
    private TextView nickname_tv;       // 昵称
    private TextView focusCount_tv;     // 关注话题数量
    private TextView fansCount_tv;      // 粉丝数量
    private Button myPost_btn;          // 我的帖子
    private Button login_btn;           // 登录
    private boolean isLogin;

    public HeadView(Context context) {
        super(context);
        this.context = context;
        initView();
//        requestHistory();
    }

    public void initView() {
        view = LayoutInflater.from(context).inflate(R.layout.view_forum_head, null);
        headportrait_iv = (ImageView) view.findViewById(R.id.forum_head_headportrait);
        center_ll = (LinearLayout) view.findViewById(R.id.forum_head_center_ll);
        nickname_tv = (TextView) view.findViewById(R.id.forum_head_nickname);
        focusCount_tv = (TextView) view.findViewById(R.id.forum_head_focuscount);
        fansCount_tv = (TextView) view.findViewById(R.id.forum_head_fanscount);
        myPost_btn = (Button) view.findViewById(R.id.forum_head_mypost);
        login_btn = (Button) view.findViewById(R.id.forum_head_login_btn);

        myPost_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ForumMyPostActivity.class);
                context.startActivity(intent);
            }
        });

        login_btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                actionLogin();
            }
        });

        this.addView(view);
    }

    public void setUserInfo(ForumUserInfoBean info) {
        if (info != null) {
            String alias = info.getUser().getAlias();
            if (TextUtil.isEmpty(alias)) {
                nickname_tv.setText(info.getUser().getUser_name());
            } else {
                nickname_tv.setText(alias);
            }
            focusCount_tv.setText(info.getUser().getTopic_focus_count());
            fansCount_tv.setText(info.getUser().getFans_count());
            String photoUrl = info.getUser().getAvatar_file();
            if (!TextUtil.isEmpty(photoUrl)) {
                headportrait_iv.setPadding(0, 0, 0, 0);
                Glide.with(getContext()).load(photoUrl)
                        .diskCacheStrategy(DiskCacheStrategy.DATA)
                        .into(headportrait_iv);
            } else {
                int padding = Utils.dip2px(5);
                headportrait_iv.setPadding(padding, padding, padding, padding);
            }
        }
    }

    public void setLogin(boolean isLogin) {
        if (isLogin) {
            login_btn.setVisibility(View.GONE);
            center_ll.setVisibility(View.VISIBLE);
            myPost_btn.setVisibility(View.VISIBLE);
        } else {
            login_btn.setVisibility(View.VISIBLE);
            center_ll.setVisibility(View.GONE);
            myPost_btn.setVisibility(View.INVISIBLE);
        }
    }

    public void actionLogin() {
        AppLotteryInfos.clear();
        ViewCallBack.getInstance().countDownObjs.clear();
        ActivityTaskManager.getInstance().closeAllActivity();
        MySharedPreferences.getInstance().setLoginStatus(false);
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }


}
